/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package generadorva;

/**
 *
 * @author USUARIO
 */
public class Binomial {

    private int n;
    private double p;

    public Binomial(int n, double p) {
        this.n = n;
        this.p = p;
    }

    public int generate (double u){
        int x = 0;
        double pr = Math.pow((1-p), n);
        double Fx = pr;
        while (u > Fx) {
            x++;
            pr = pr*p/(1-p)*(n-(x-1))/x;
            Fx += pr;
        }

        return x;
    }

    public int getN() {
        return n;
    }

    public double getP() {
        return p;
    }
    
}
